package com.briup.smartlabs.service.impl;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;

import com.briup.smartlabs.common.exceptions.DataValidateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.smartlabs.bean.ex.Device;
import com.briup.smartlabs.mapper.ex.DeviceMapper;
import com.briup.smartlabs.service.SmartDeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SmartDeviceServiceImpl implements SmartDeviceService {
	@Autowired
	private DeviceMapper deviceExMapper;

	@Override
	public PageInfo<Device> findByCondition(int pageNum, int pageSize, String typeId, String key) {
		PageHelper.startPage(pageNum, pageSize);
		List<Device> byCondition = deviceExMapper.findByCondition(typeId, key);
		return new PageInfo<>(byCondition);
	}

	@Override
	public void sendMessage(String command) {
		//获取设备所属实验室的ip和端口
		try (Socket socket = new Socket("localhost", 9999);
			 PrintStream ps = new PrintStream(
					 socket.getOutputStream())) {
			ps.println(command);
			ps.flush();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void removeById(String id) {
		if (id == null || id.equals("")) {
			throw new DataValidateException("设备id为空！");
		}
		Device device = deviceExMapper.selectById(id);
		if (device == null) {
			throw new DataValidateException("该设备不存在!");
		}
		deviceExMapper.deleteById(id);
	}

	@Override
	public Map<String, List<String>> removeByBatch(String[] ids) {
		if (ids == null || ids.length == 0) {
			throw new DataValidateException("参数为空");
		}
		List<String> success = deviceExMapper.findByIds(ids);
		if (success != null || !success.isEmpty()) {
			deviceExMapper.deleteByBatch(success);
		}
		List<String> failIds = new ArrayList<>();
		for (String id : ids) {
			if (!success.contains(id)) {
				failIds.add(id);
			}
		}
		Map<String, List<String>> map = new HashMap<>();
		map.put("success", success);
		map.put("fail", failIds);
		return map;
	}

	@Override
	public void saveOrUpdate(Device dev) {
		//参数判断
		if (dev == null) {
			throw new DataValidateException("参数为空");
		}
		//注意：设备id必须唯一
		String devAddr = dev.getDeviceAddr();
		//判断如果用户输入的设备id不为空，代表需要执行更新操作
		String id = dev.getId();
		if (id != null && !"".equals(id)) {
			//判断id是否存在且有效
			Device smartDevice = deviceExMapper.selectById(id);
			if (smartDevice == null)
				throw new DataValidateException("更新失败：设备id不存在！");
//如果要修改设备地址，但设备地址已经存在，则更新失败
			if (devAddr != null && !devAddr.equals(smartDevice.getDeviceAddr()) && deviceExMapper.getCountByDeviceAddr(devAddr) > 0) {
				throw new DataValidateException("设备地址已经存在!");

			}
			//执行更新操作
			deviceExMapper.updateById(dev);
		} else {
			//参数中没有设备的id，代表执行保存操作
//设备地址唯一判断
			if (devAddr != null &&
					deviceExMapper.getCountByDeviceAddr(devAddr) > 0)
				throw new DataValidateException("设备地址已经存在!");
//生成唯一且不重复的id值
			dev.setId(UUID.randomUUID().toString());
//新增设备
			deviceExMapper.insert(dev);
		}
	}
}




